package com.pda.xinma.presenter;

import com.pda.xinma.bean.BackDepotListBean;
import com.pda.xinma.bean.BackInfoBean;
import com.pda.xinma.bean.HnadingInDetailListBean;
import com.pda.xinma.contract.BackDepotContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class BackDepotPresenter extends BasePresenter<BackDepotContract.View> implements BackDepotContract.Presenter{
    @Override
    public void queryBackInfo(String container,String rollerId) {
        NetUtils.geApi().queryBackInfo(container,rollerId)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<BackInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<BackInfoBean> backInfoBeanBaseAppEntity) throws Exception {
                        getView().setData(backInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void addBackEntry(String container, String weight,  String rollerId, String actualWeight) {
        NetUtils.geApi().addBackEntry(container, weight,rollerId,actualWeight)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<BackInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<BackInfoBean> backInfoBeanBaseAppEntity) throws Exception {
                        getView().showMessage(backInfoBeanBaseAppEntity.getMessage());
                            getView().finishActivity();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectBackList(String taskNo, Integer pageNum) {
        NetUtils.geApi().selectBackList(taskNo,pageNum,10)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<BackDepotListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<BackDepotListBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
    @Override
    public void entryClose(String entryNo) {
        NetUtils.geApi().entryClose(entryNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<HnadingInDetailListBean>>() {
                    @Override
                    public void accept(BaseAppEntity<HnadingInDetailListBean> hnadingInDetailListBeanBaseAppEntity) throws Exception {
                        getView().showMessage(hnadingInDetailListBeanBaseAppEntity.getMessage());
                        getView().closeSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void queryBackInfoByNo(String entryNo) {
        NetUtils.geApi().queryBackInfoByNo(entryNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<BackInfoBean>>() {
                    @Override
                    public void accept(BaseAppEntity<BackInfoBean> backInfoBeanBaseAppEntity) throws Exception {
                        getView().setData(backInfoBeanBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
